package org.example.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.example.domain.CarInfo;
import org.example.service.CarInfoService;

import java.util.List;

public class CarInfoListener implements ReadListener<CarInfo> {
   private static final int BATCH_COUNT = 3;
   private List<CarInfo> carInfoList=ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
   private CarInfoService carInfoService;
   public CarInfoListener(CarInfoService carInfoService){
       this.carInfoService=carInfoService;
   }
    @Override
    public void invoke(CarInfo carInfo, AnalysisContext analysisContext) {
        System.out.println("解析出来的数据"+carInfo);
        carInfoList.add(carInfo);

        if(carInfoList.size()>=BATCH_COUNT){
            System.out.println("list:"+carInfoList);
            carInfoService.saveBatch(carInfoList);
            carInfoList=ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("解析完成");
        if(carInfoList.size()>=1){
            carInfoService.saveBatch(carInfoList);
            System.out.println("list"+carInfoList);
        }

    }
}
